package com.nut.lmsc.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nut.lmsc.common.Result;
import com.nut.lmsc.entity.Cat;
import com.nut.lmsc.entity.Category;
import com.nut.lmsc.mapper.CatMapper;
import com.nut.lmsc.mapper.CategoryMapper;
import com.nut.lmsc.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private CatMapper catMapper;

    @Override
    public Result<IPage<Category>> listPage(String name, Integer status, Integer pageNo, Integer pageSize) {
        Page page = new Page(pageNo, pageSize);
        IPage<Category> list = categoryMapper.listPage(page, name, status);
        return Result.ok(list);
    }

    @Override
    public Result<List<Category>> list() {
        List<Category> list = categoryMapper.selectByPid(0);
        return Result.ok(list);
    }

    @Override
    public Result<List<Category>> listByPid(Integer pid) {
        List<Category> list = categoryMapper.selectByPid(pid);
        List<Category> child = getChild(pid, list);
        return Result.ok(child);
    }

    @Override
    public Result<List<Category>> getTree(Integer pid) {
        List<Category> categoryList = categoryMapper.selectList(null);
        List<Category> child = getChild(pid, categoryList);
        return Result.ok(child);
    }

    private List<Category> getChild(Integer pid, List<Category> categoryList) {
        List<Category> childList = new ArrayList<>();
        for (Category category : categoryList) {
            Integer catPid = category.getPid();
            if (catPid.equals(pid)) {
                childList.add(category);
            }
        }

        for (Category category : childList) {
            Integer id = category.getId();
            category.setSub(getChild(id, categoryList));
        }

        return childList;
    }

    @Override
    public Result<Category> getById(Serializable id) {
        Category category = categoryMapper.selectById(id);
        return Result.ok(category);
    }

    @Override
    public Result add(Category category) {
        categoryMapper.insert(category);
        return Result.ok();
    }

    @Override
    public Result updateById(Category category) {
        categoryMapper.updateById(category);
        return Result.ok();
    }

    @Override
    public Result deleteByIds(Set<Integer> ids) {
        for (Integer id : ids) {
            categoryMapper.deleteById(id);
        }
        return Result.ok();
    }

    @Override
    public void importData() {

//        List<Cat> cats = catMapper.selectAll();
//
//        //导入第一级
//        String n1 = null;
//        for (Cat cat : cats) {
//            if (cat.getN1().equals(n1)) {
//                continue;
//            }
//            n1 = cat.getN1();
//
//            Category category = new Category();
//            category.setName(cat.getN1());
//            category.setPid(0);
//            category.setLevel(1);
//            category.setStatus(1);
//            if (cat.getN4().equals("实物")) {
//                category.setNature(1);
//            } else {
//                category.setNature(2);
//            }
//            categoryMapper.insert(category);
////            headMap.put(n1, category.getId());
//        }


        List<Category> head = categoryMapper.selectByPid(0);

        HashMap<String, Integer> headMap = new HashMap<>();
        for (Category category : head) {
            headMap.put(category.getName(), category.getId());
        }

        List<Cat> cats = catMapper.selectAll();

        String n2 = null;
        Integer n2Id = null;
        for (Cat cat : cats) {

            if (!cat.getN2().equals(n2)) {

                n2 = cat.getN2();

                Integer pid = headMap.get(cat.getN1());

                Category cat2 = new Category();
                cat2.setPid(pid);
                cat2.setName(cat.getN2());
                cat2.setStatus(1);
                cat2.setLevel(2);
                if (cat.getN4().equals("实物")) {
                    cat2.setNature(1);
                } else {
                    cat2.setNature(2);
                }
                categoryMapper.insert(cat2);

                n2Id = cat2.getId();
            }

            //第三级
            Category cat3 = new Category();
            cat3.setPid(n2Id);
            cat3.setName(cat.getN3());
            cat3.setStatus(1);
            cat3.setLevel(3);
            if (cat.getN4().equals("实物")) {
                cat3.setNature(1);
            } else {
                cat3.setNature(2);
            }
            categoryMapper.insert(cat3);

        }

    }

}